home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / amislate / slaterexx / displaytext.rexx < prev    next >
OS/2 REXX Batch file  |  1995-08-05  |  9KB  |  339 lines

  1. /* TextDisplay.rexx
  2.  
  3.    An ARexx script designed to work with AmiSlate.
  4.    
  5.    Lets you write text on the canvas.
  6.  
  7. */
  8. parse arg CommandPort ActiveString
  9.  
  10. if (length(CommandPort) == 0) then do
  11.     say ""
  12.     say "Usage:  rx TextDisplay.rexx <REXXPORTNAME>"
  13.     say "        (REXXPORTNAME is usually AMISLATE)"
  14.     say ""
  15.     say "Or run from the Rexx menu within AmiSlate."
  16.     say ""
  17.     exit 0
  18.     end
  19.     
  20.  
  21. address (CommandPort)
  22. options results
  23.  
  24. /* change this to use different fonts */
  25. DefaultSlateFont = "SlateRexx:simple.slatefont"
  26.  
  27. /* Constants for use with AmiSlate's ARexx interface */
  28. AMessage.TIMEOUT     = 1    /* No events occurred in specified time period */
  29. AMessage.MESSAGE     = 2    /* Message recieved from remote Amiga */
  30. AMessage.MOUSEDOWN   = 4    /* Left mouse button press in drawing area */
  31. AMessage.MOUSEUP     = 8    /* Left mouse button release in drawing area */
  32. AMessage.RESIZE      = 16    /* Window was resized--time to redraw screen? */
  33. AMessage.QUIT        = 32    /* AmiSlate is shutting down */
  34. AMessage.CONNECT     = 64    /* Connection established */
  35. AMessage.DISCONNECT  = 128    /* Connection broken */
  36. AMessage.TOOLSELECT  = 256    /* Tool Selected */
  37. AMessage.COLORSELECT = 512    /* Palette Color selected */
  38. AMessage.KEYPRESS    = 1024    /* Key pressed */
  39. AMessage.MOUSEMOVE   = 2048     /* Mouse was moved */
  40.  
  41. CHAR_ESCAPE = 27
  42.  
  43.  
  44. /* initial values--this WaitEvent also makes sure no character from before
  45.    the ARexx script was started is going to be given to us later */
  46. WaitEvent 1 stem evt.
  47. oldX = evt.mousex
  48. oldY = evt.mousey
  49.  
  50. sWidth  = 100
  51. sHeight = 30
  52. oldWidth = sWidth
  53. oldHeight = sHeight
  54.  
  55. TextX = 0
  56. TextY = 0
  57. TextWidth = 0
  58. TextHeight = 0
  59.  
  60. TryAgain = 1
  61.  
  62. lock on
  63.  
  64. word = d2c(1)
  65.  
  66. loadstring = '"' || "Loading font: [" || DefaultSlateFont || "]" || '"'
  67. SetWindowTitle loadstring
  68.  
  69. if ~LoadSlateFont(DefaultSlateFont) then do
  70.     EasyRequest '"'||"TextDisplay Error"||'"' '"'||"Couldn't load "||DefaultSlateFont||'"' '"'||"Abort"||'"'
  71.     lock off
  72.     exit
  73.     end    
  74.  
  75. do while (TryAgain == 1)
  76.     /* start things off */
  77.     square (oldX-oldWidth) (oldY-oldHeight) oldX oldY XOR
  78.     firstkey = PositionCursor() 
  79.     if (firstkey == CHAR_ESCAPE) then do
  80.         SetWindowTitle '"'||"TextDisplay.rexx aborting, bye!"||'"'
  81.         lock off
  82.         exit
  83.         end
  84.     SetWindowTitle '"'||"Enter text, or press escape when done."||'"'    
  85.     
  86.     StringRequest stem msg. '"'||"TextDisplay Request"||'"' '"'||word||'"' '"'||"Enter your text now"||'"'
  87.     word = msg.message
  88.  
  89.     if ((length(word) == 0)|(word = "(User Aborted)")|(word = "MSG.MESSAGE")) then do
  90.         lock off
  91.         say "aborting!"
  92.         exit
  93.         end
  94.         
  95.     SetWindowTitle '"'||"Previewing word, please wait..."||'"'
  96.     success = DrawWord(word, TextX, TextY, TextWidth, TextHeight, 1)    
  97.  
  98.     EasyRequest '"'||"TextDisplay Request"||'"' '"'||"Is this text okay?"||'"' '"'||"Accept & Repeat|Accept|Redo|Cancel"||'"'
  99.     if (rc == 1) then do
  100.         SetWindowTitle '"'||"Drawing word, please wait..."||'"'
  101.         success = DrawWord(word, TextX, TextY, TextWidth, TextHeight, 0)    
  102.         TryAgain = 1
  103.         end
  104.     if (rc == 2) then do
  105.         SetWindowTitle '"'||"Drawing word, please wait..."||'"'
  106.         success = DrawWord(word, TextX, TextY, TextWidth, TextHeight, 0)    
  107.         SetWindowTitle '"'||"All done, TextDisplay script exiting."||'"'
  108.         TryAgain = 0
  109.         end
  110.     if (rc == 3) then do
  111.         SetWindowTitle '"'||"Erasing word, please wait..."||'"'
  112.         success = DrawWord(word, TextX, TextY, TextWidth, TextHeight, 1)    
  113.         TryAgain = 1
  114.         end
  115.     if (rc == 0) then do
  116.         SetWindowTitle '"'||"Erasing word, please wait..."||'"'
  117.         success = DrawWord(word, TextX, TextY, TextWidth, TextHeight, 1)    
  118.         SetWindowTitle '"'||"All done, TextDisplay script exiting."||'"'
  119.         TryAgain = 0
  120.         end
  121.     end
  122. lock off
  123. exit
  124.  
  125.  
  126. WordWidth: procedure expose SlateFont.
  127.     parse arg word
  128.     
  129.     width = 0
  130.     
  131.     do while (length(word) > 0)
  132.         nextletter = left(word,1)
  133.         word = right(word, length(word)-1)
  134.         
  135.         width = width + LetterWidth(nextletter)
  136.         end
  137.         
  138.     return width
  139.     
  140.  
  141. /* Procedure to draw a word. */
  142. DrawWord: procedure expose SlateFont.
  143.     parse arg word, x, y, width, height, BXor
  144.  
  145.     wordlength   = length(word)    
  146.     wordcolumns  = WordWidth(word)
  147.  
  148.     maxletterwidth = 8 * (width / wordcolumns)
  149.  
  150.     letter = 1
  151.     letterx = x
  152.     do while (letter <= wordlength)
  153.         nextletter = left(word,letter)
  154.         nextletter = right(nextletter,1)
  155.         width = trunc(LetterWidth(nextletter) / 8 * maxletterwidth)
  156.         sux = DrawLetter(nextletter, letterx, y, maxletterwidth, height, BXor)
  157.         letter = letter + 1        
  158.         letterx = letterx + width
  159.         end
  160.         
  161.     return 1
  162.  
  163.  
  164. LetterWidth : procedure expose SlateFont.
  165.     parse arg nextletter
  166.  
  167.     asciicode = c2d(nextletter)
  168.     nextletter = SlateFont.asciicode
  169.     widthcode = left(nextletter, 2)    
  170.     if (left(widthcode,1) ~= "W") then return 8
  171.     return(right(widthcode,1))
  172.     
  173.     
  174.  
  175. DrawLetter : procedure expose SlateFont.
  176.     parse arg letter, x, y, w, h, BXor
  177.  
  178.     /* Set our fcolor to user's fcolor */
  179.     GetStateAttrs stem stateattr.
  180.     SetFPen stateattr.fpen
  181.     
  182.     letter = left(letter, 1)    
  183.     asciicode = c2d(letter)
  184.     drawcode = SlateFont.asciicode
  185.  
  186.     lastcode = "X5"
  187.     
  188.     /* If "S" then we've got an unidentified char */
  189.     if (left(drawcode,1) == "S") then do
  190.         if (BXor) then do
  191.             circle (x+trunc(0.4*w)) (y+trunc(0.4*h)) (trunc(w*0.2)) (trunc(h*0.2)) XOR
  192.             end
  193.         else do
  194.             circle (x+trunc(0.4*w)) (y+trunc(0.4*h)) (trunc(w*0.2)) (trunc(h*0.2)) 
  195.             end    
  196.         return 1
  197.         end
  198.     do while (length(drawcode) > 0)
  199.         nextcode = left(drawcode,2)
  200.         if (length(drawcode) > 2) then do
  201.             drawcode = right(drawcode, length(drawcode)-3)
  202.             end
  203.             else do
  204.             drawcode = ""
  205.             end    
  206.         horizcoord = c2d(left(nextcode,1))-65
  207.         vertcoord  = c2d(right(nextcode,1))-48
  208.         
  209.         /* Ignore any "W"'s */
  210.         if (horizcoord ~= 22) then do
  211.             /* If "X" then penreset */
  212.             if (horizcoord == 23) then do
  213.                 PenReset
  214.                 end
  215.             else do
  216.                 if (nextcode == lastcode) then do
  217.                     if BXor then do
  218.                         circle (x+trunc(horizcoord * w / 10)) (y+trunc(vertcoord * h / 10)) (trunc(w*0.1)) (trunc(h*0.05)) XOR
  219.                         end
  220.                     else do
  221.                         circle (x+trunc(horizcoord * w / 10)) (y+trunc(vertcoord * h / 10)) (trunc(w*0.1)) (trunc(h*0.05))
  222.                         end
  223.                 end
  224.                 else 
  225.                 do
  226.                     if BXor then do
  227.                         pen (x+trunc(horizcoord * w / 10)) (y+trunc(vertcoord * h / 10)) XOR
  228.                         end
  229.                     else do
  230.                         pen (x+trunc(horizcoord * w / 10)) (y+trunc(vertcoord * h / 10)) 
  231.                         end
  232.                 end
  233.     
  234.                 lastcode = nextcode        
  235.     
  236.                 end
  237.             end
  238.         end    
  239.         
  240.     return 1
  241.  
  242.  
  243. /* procedure to get a starting size and position for the cursor */
  244. PositionCursor: procedure expose oldX oldY oldWidth oldHeight BSizeMode AnchorLeft AnchorTop sWidth sHeight AMessage. TextX TextY TextWidth TextHeight
  245.     done = 0
  246.     BSizeMode = 0
  247.     
  248.     PosModeString = '"'||"Positioning mode:  Move mouse to position, hold button to size, press RETURN when done." || '"'
  249.     SizeModeString = '"'||"Sizing mode:  Move mouse to size, release button to position, press RETURN when done." || '"'
  250.     
  251.     SetWindowTitle PosModeString
  252.     do while (done=0)
  253.         WaitEvent QUIT KEYPRESS MOUSEMOVE RESIZE stem e.
  254.         if (e.type = AMessage.QUIT) then exit
  255.         if (e.lastkey > 0) then done = 1
  256.  
  257.         if ((BSizeMode = 0)&(e.button ~= 0)) then do 
  258.             SetWindowTitle SizeModeString
  259.             AnchorTop = e.mousey - sHeight
  260.             AnchorLeft = e.mousex - sWidth
  261.             BSizeMode = 1
  262.             end
  263.         if ((BSizeMode = 1)&(e.button = 0)) then do 
  264.             SetWindowTitle PosModeString
  265.             BSizeMode = 0
  266.             end
  267.  
  268.         success = DrawCursor(e.mousex, e.mousey)
  269.         e.type = 0
  270.         end
  271.     /* erase cursor at last */
  272.     square (oldX-OldWidth) (oldY-OldHeight) oldX oldY XOR
  273.     TextX = oldX-OldWidth
  274.     TextY = oldY-OldHeight
  275.     TextWidth = OldWidth
  276.     TextHeight= OldHeight
  277.     
  278.     /* Get current window height & width */
  279.         GetWindowAttrs stem winattrs.
  280.         CanvasWidth = winattrs.width  - 58
  281.         CanvasHeight= winattrs.height - 53
  282.     
  283.     /* clip! */
  284.     if (TextX < 0) then TextX = 0
  285.     if (TextY < 0) then TextY = 0
  286.     if (TextX >= CanvasWidth) then return CHAR_ESCAPE
  287.     if (TextY >= CanvasHeight) then return CHAR_ESCAPE
  288.     if ((TextWidth + TextX) > CanvasWidth) then TextWidth = (CanvasWidth - TextX)
  289.     if ((TextHeight+ TextHeight) > CanvasHeight) then TextHeight = (CanvasHeight - TextY)
  290.     return e.lastkey
  291.  
  292.  
  293.  
  294. /* Procedure to draw the cursor square */
  295. DrawCursor:  procedure expose oldX oldY oldWidth oldHeight BSizeMode AnchorLeft AnchorTop sWidth sHeight
  296.     parse arg X, Y
  297.  
  298.     if ((X == oldX)&(Y == OldY)&(sWidth == oldWidth)&(sHeight == oldHeight)) then return 1
  299.     
  300.     /* First erase old square */
  301.     square (oldX-OldWidth) (oldY-OldHeight) oldX oldY XOR 
  302.         
  303.     if (BSizeMode == 1) then do
  304.         sWidth = X - AnchorLeft
  305.         sHeight = Y - AnchorTop
  306.         
  307.         if (sWidth < 0) then sWidth = 0
  308.         if (sHeight < 0) then sHeight = 0
  309.     end
  310.  
  311.     /* Now draw the new square */
  312.     square (X-sWidth) (Y-sHeight) X Y XOR 
  313.  
  314.     /* Now set the old coords for next time */
  315.     oldX = X
  316.     oldY = Y
  317.     oldHeight = sHeight
  318.     oldWidth  = sWidth
  319.     
  320.     return 1
  321.     
  322.  
  323.     
  324. /* Loads a font in from disk */
  325. LoadSlateFont: procedure expose SlateFont.
  326.       parse arg InputFile .
  327.  
  328.       /* Do very simple error checking                              */
  329.       if InputFile = '' then return 'ERROR'
  330.       if ~open(fontfile, InputFile, 'R') then return 0
  331.  
  332.       /* Read all lines in input file                               */
  333.       do until eof(fontfile)
  334.                 ThisLine = readln(fontfile)
  335.                 if (left(ThisLine,1) == ".") then Interpret("SlateFont"||ThisLine)
  336.       end
  337.       call close fontfile;
  338.       return 1
  339.